import cv2
import numpy as np


class Padding:
    def __init__(self, type):
        self.type = type
    
    def __call__(self, src, size):
        sh, sw = src.shape
        m = (size - 1) // 2

        h = sh + size - 1
        w = sw + size - 1

        dst = np.zeros((h, w), dtype='uint8')

        dst[m:m+sh, m:m+sw] = src

        for y in range(m, m + sh):
            dst[y, : m] = dst[y, m]
            dst[y, m + sh:] = dst[y, m + sw - 1]
        
        for x in range(m, m + sw):
            dst[: m, x] = dst[m, x]
            dst[m + sw:, x] = dst[m + sh - 1, x]

        dst[0:m, 0:m] = dst[m, m]
        dst[m + sh: 2 * m + sh, 0:m] = dst[m + sh - 1, m]
        dst[0:m, m + sw: 2 * m + sw] = dst[m, m + sw - 1]
        dst[m + sh: 2 * m + sh, m + sw: 2 * m + sw] = dst[m + sh - 1, m + sw - 1]

        return dst

if __name__ == '__main__':
    SRC = cv2.imread('lena.bmp', cv2.IMREAD_GRAYSCALE)

    def show(winname, img):
        cv2.imshow(winname, img)
        cv2.resizeWindow(winname, 512, 512)
        cv2.waitKey(1)

    show('padding', Padding('REPEAT')(SRC, 3))

    cv2.waitKey(0)